<?php
/**
 * Created by PhpStorm.
 * User: alei
 * Date: 11/4/2017 AD
 * Time: 16:17
 */
namespace App\Http\Controllers\MobileApi;

use Storage;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Mysql\pre_ct_ui;
use App\Models\Mysql\pre_ct_task;
use App\Models\Mysql\pre_forum_pollvoter;
use App\Models\Mysql\table_n;
use App\Models\Mysql\pre_forum_thread;
use App\Models\Mysql\pre_ucenter_members;
use App\Models\Mysql\pre_ct_translatedtext;
use App\Models\Mysql\pre_forum_post;

class TranslationController extends Controller
{
	use GlobalData;
    private $url="http://47.94.110.155:8081/";
	const ROUTE_PREFIX = '/api/*';

	protected $request;

	protected static $forumNameMapFID;

	protected $curTime;



	/**
	 * 创建新的home控制器实例
	 * @param \Illuminate\Http\Request $request
	 */
	public function __construct(Request $request)
	{
		$this->request = $request;
		$this->error = [];
		$this->curTime = time();

		self::$forumNameMapFID = $this->getForumNameMapFID();

		if(config('app.debug')) {
			error_reporting(E_ALL | E_STRICT);
		}
	}



	public  function  ce()
    {


        $param=['tid'=>126];

        $b=[132,133];
        $ids=implode(',',$b);
        $cc=db()->query("SELECT * from pre_ct_translatedtext where tid in ($ids)");
//        $vote_list=pre_forum_pollvoter::option($param);
//        echo '<pre>';
//        $a=[];
//        foreach ($vote_list as $k=>$v)
//        {
//            $a[]=$v->polloptionid;
//
//        }
//        $b=implode("\t", $a);
//        var_dump($b);
//        print_r($cc);die;


//        $r=session('uid');
//        file_put_contents('1.log','时间：'.date('Y-m-d H:i:s',time())."\n". var_export($_REQUEST,true),8);
        session_start();
        if(!$_SESSION)
        {
            $data = array(
                'code' => 1,
                'msg' => '亲，请登录',
                'result' => [],
            );
            exit(json_encode($data));
        }
        $data = array(
            'code' => 200,
            'msg' => '好了',
            'result' => [1,2,6,8],
        );
        exit(json_encode($data));
    }



    #任务点评译文接口
    #传任务tid=130
    public function  original_article()
    {
        $p = $_REQUEST;
        $param['tid']=isset($p['tid']) ?  (int)$p['tid'] : '';
        if( empty($param) )
        {
            $data = array(
                'code' => 5,
                'msg' => '参数不全',
            );
            exit(json_encode($data));
        }
        $data=pre_ct_task::get_task($param);
		
		if (!empty($data)){
			$tmp_message = $data[0]->message;
			$tmp_message = str_replace("[//]"," ",$tmp_message);
			$tmp_message = messagecutstr($tmp_message);
			/*$tmp_message = discuzcode($tmp_message,1,0);
			$tmp_message = str_replace("\r\n"," ",$tmp_message);*/
			$data[0]->message = $tmp_message;
			
			$data=json_decode(json_encode($data),true);
			$data[0]['difficulty']=$this->rank($data[0]['difficulty']);
			$data[0]['commentendtime']=$this->totime(date('Y-m-d H:i:s',$data[0]['commentendtime']));
			$count=pre_ct_task::get_count($data[0]['taskid']);
			$data[0]['total_translators']=$count[0]->count;
			$data[0]['tid']=$param['tid'];
			
			$curUser = getCurUser();
			if (empty($curUser)){
				$param['uid'] = 0;
				$data[0]['curUserAlreadyVoted'] = false;
			}
			else{
				$param['uid'] = $curUser['uid'];
				$data[0]['curUserAlreadyVoted'] = !empty(pre_forum_pollvoter::find_vote($data[0]['polltid'],$param));
			}
	
			$data = array(
				'code' => 200,
				'msg' => '任务原文数据返回成功',
				'result' => $data,
			);
		}
		else{
			$data = array(
				'code' => 200,
				'msg' => '任务原文数据返回成功',
				'result' => [],
			);
		}
        exit(json_encode($data));
        //echo '<pre>';
        //print_r($data);

    }

    #计算倒计时
    public  function  totime($enddate)
    {
        $startdate=date('Y-m-d H:i:s',time());
        $date=floor((strtotime($enddate)-strtotime($startdate))/86400);
        $hour=floor((strtotime($enddate)-strtotime($startdate))%86400/3600);
        $minute=floor((strtotime($enddate)-strtotime($startdate))%86400/60);
        $second=floor((strtotime($enddate)-strtotime($startdate))%86400%60);
        $a=$date."天".$hour."小时".$minute."分钟".$second."秒";
        return $a;
    }

    public function rank($rank)
    {
       if($rank=='1')
       {
           $a = '中级';
       }
       if($rank=='2')
       {
          $a='高级';
       }
       if($rank=='0')
       {
           $a= '初级';
       }
       return $a;
    }

    #获取任务翻译列表
    public function lst()
    {
        $p = $_REQUEST;
        $page =isset($p['page']) ?  (int)$p['page'] : '';
        $num = isset($p['num']) ?  (int)$p['num'] : '';
        $uid=isset($p['uid']) ?  (int)$p['uid'] : '';
        $tid=isset($p['tid']) ?  (int)$p['tid'] : '';#任务的pid 在帖子表里
        $pid=isset($p['pid']) ?  (int)$p['pid'] : '';#任务的pid 在帖子表里


        if( empty($tid) || empty($pid))
        {
            $data = array(
                'code' => 5,
                'msg' => '参数不全',
            );
            exit(json_encode($data));
        }
        $num=!empty($num) ? $num : 10;
        $page=!empty($page) ? $page : 1;
        $limit_start=($page-1)*$num;
        $limt_end= $num;
        $param=['uid'=>$uid,'start'=>$limit_start,'end'=>$limt_end,'tid'=>$tid,'pid'=>$pid];
        $data=$this->list_data($param,$page,$num);

        if(empty($data['result']))
        {
            $data = array(
                'code' => 200,
                'msg' => '亲，暂无数据',
                'result' => []
            );

        }
        exit(json_encode($data));
    }

    public function list_data($param,$page,$num)
    {

        $taskid=pre_ct_task::get_taskid($param);
        if(empty($taskid))
        {
            $data = array(
                'code' => 200,
                'msg' => '亲，没有该任务',
                'result' => []
            );
            exit(json_encode($data));
        }
        $taskid=$taskid[0]->taskid;
        $data=pre_forum_post::get_list($param);
		foreach($data as $k=>$v){
			$tmp_message = $v->message;
			$tmp_message =str_replace("[//]"," ",strip_tags($tmp_message));
			$v->message = messagecutstr($tmp_message);			
		}
        $data=json_decode(json_encode($data),true);
        $count=pre_ct_task::get_count($param);
        $count=json_decode(json_encode($count),true);
        if(empty($data))
        {
            $data2=[];
            $count=array(0=>array('count'=>1,'page'=>1));
            $data2['count']=$count[0]['count'];
            $data2['page']=$count[0]['page'];
            $result=[];

        }
        else
        {
            $data2=[];
            $count=ceil($count[0]['count']/$num);
            $count=array(0=>array('count'=>$count,'page'=>$page));
            $data=$this->link($data,$param,$taskid);
            $data2['count']=$count[0]['count'];
            $data2['page']=$count[0]['page'];
            $result=$data;
        }

        $data1 = array(
            'code' => 200,
            'msg' => '数据返回成功',
            'count'=>$data2['count'],
            'page'=>$data2['page'],
            'result' => $result
        );
        return $data1;
    }

    public function  link($data,$param,$taskid)
    {
        foreach($data as $k=>$v)
        {
            $data[$k]['head_img']=$this->url.'66.jpg';
            $data[$k]['taskid']=$taskid;
            $data[$k]['dateline']=date('Y-m-d H:i:s',$v['dateline']);
            $data[$k]['vote_status']=pre_forum_pollvoter::find_vote($v['tid'],$param);
        }
        return $data;
    }

    #取出某个任务的所有译文列表
   public function TranslationListOfTask()
   {
       $p = $_REQUEST;
       $param['srctid']=isset($p['srctid']) ?  (int)$p['srctid'] : '';
	   
       if( empty($param['srctid']))
       {
           $data = array(
               'code' => 5,
               'msg' => '参数不全',
           );
           exit(json_encode($data));
       }
		
		$r = pre_forum_post::getTranslationListOfTask($param['srctid']);
		foreach($r as $v){
			$v->message = messagecutstr(str_replace("[//]"," ",$v->message));
			$v->head_img = avatarUrl($v->authorid);
			$v->dateline = date('Y-m-d H:i:s',$v->dateline);
		}
		
		return json_encode( array(
			'code' => 200,
			'msg' => '此任务的翻译列表数据返回成功',
			'result' => $r,
			));
   }

    #取出中用户翻译详情，中文和英文对比
   public function detail()
   {
       $p = $_REQUEST;
       $param['taskid']=isset($p['taskid']) ?  (int)$p['taskid'] : '';
       $param['pid']=isset($p['pid']) ?  (int)$p['pid'] : '';
       $param['tid']=isset($p['tid']) ?  (int)$p['tid'] : '';
	   
       if( empty($param['pid']) || empty($param['taskid']) || empty($param['tid']))
       {
           $data = array(
               'code' => 5,
               'msg' => '参数不全',
           );
           exit(json_encode($data));
       }
       
	   //added by lishu(spcial to adapt front-back end interface)
	   if ( true || $param['taskid'] == -1 ){
			$r = pre_ct_translatedtext :: get_info_by_tid($param['tid']);
			if ($r){
				$param['taskid'] = $r[0] -> taskid;
				$isPolishedBest = false;
				$taskInfo = pre_ct_task::getSingleInfoByKV('taskid',$param['taskid']);
			}
			else{
				$r = pre_ct_task :: get_info_by_polishedbesttid($param['tid']);
				$taskInfo = $r[0];
				if ($r){
					$param['taskid'] = $r[0] -> taskid;
					$isPolishedBest = true;
				}
				else{
					$data = array(
						'code' => 6,
						'msg' => 'tid为'.$param['tid'].'的译文不存在',
					);
					exit(json_encode($data));
				}
			}
	   }
	   	$taskStatus = $taskInfo->status;
		$polltid = $taskInfo->polltid;

	   
	   if ( $param['pid'] == -1 ){
		   $r = pre_forum_post::get_pid_forum_post($param);
		   $param['pid'] = $r[0]->pid;
	   }
	   //end of added by lishu(spcial to adapt front-back end interface)
	   
	   $curUser = getCurUser();
	   if (empty($curUser)){
			$param['uid'] = 0;
			$curUserAlreadyVoted = false;

	   }
	   else{
			$param['uid'] = $curUser['uid'];
			$curUserAlreadyVoted = !empty(pre_forum_pollvoter::find_vote($polltid,$param));
		}
		
	   
	   $data=pre_ct_task::task($param);#先从task表里取出任务的tid
       $task_data=json_decode(json_encode($data),true);

       $data=pre_ct_task::get_info($task_data[0]['srctid']);#因为tid不唯一，再去帖子表里取出任务的标题和文章 limit 1一定是翻译任务，
        #因为先有翻译任务才会有翻译和评论，所以可以确定根据tid 取出的limit 1 一定是 任务，而不是翻译和回帖
       $data=json_decode(json_encode($data),true);

       $data_post=pre_forum_post::get_info($param);#根据pid直接取出译文和翻译标题
       $data_post=json_decode(json_encode($data_post),true);
       #开始判断是否分段
       $list=[];
       $list2=[];
       $arr=[];
//       file_put_contents('1.log','数组1：'.var_export($list,true).'数组1：'.var_export($list2,true)."\n",8);
       if(strpos($data[0]['message'],"[//]"))
       {
           $list=explode("[//]",$data[0]['message']);
           $list2=explode("[//]",$data_post[0]['message']);
           $arr=$this->arr_info($list,$list2);
       }
       else
       {
           $arr[]=$data[0]['message'];
           $arr[]=$data_post[0]['message'];
           $list2[0]=$data_post[0]['message'];
       }
	   foreach($arr as $k=>$v){
		$tmp_message = $v;
		$tmp_message = messagecutstr($tmp_message);
/*		$tmp_message = str_replace("[//]"," ",$tmp_message);
		$tmp_message = discuzcode($tmp_message,1,0);
		$tmp_message = str_replace("\r\n"," ",$tmp_message);*/
		$v = $tmp_message;		   
	   }
	   foreach($list2 as $k=>$v){
		$tmp_message = $v;
		$tmp_message = messagecutstr($tmp_message);
/*		$tmp_message = str_replace("[//]"," ",$tmp_message);
		$tmp_message = discuzcode($tmp_message,1,0);
		$tmp_message = str_replace("\r\n"," ",$tmp_message);*/
		$v = $tmp_message;		   
	   }
        $data[0]['english']=$list2;
        $data[0]['all']=$arr;
        $data[0]['english_subject']=$data_post[0]['subject'];
        $data[0]['pid']=$data_post[0]['pid'];
        $data[0]['author']=$data_post[0]['author'];
        $data[0]['tid']=$data_post[0]['tid'];
        $data[0]['time']=date('Y-m-d H:i:s',$data_post[0]['dateline']);
        #添加头像
        $data[0]['head_img']=avatarUrl($data_post[0]['authorid']);
        #删除多余的信息
        unset($data[0]['message']);
        unset($data[0]['seriesid']);
        unset($data[0]['status']);
        unset($data[0]['publishtime']);
        unset($data[0]['submittime']);
        unset($data[0]['commentendtime']);
        unset($data[0]['srctid']);
        unset($data[0]['polltid']);
        unset($data[0]['besttid']);
        unset($data[0]['polishedbesttid']);
        unset($data[0]['maxsubmitnumber']);
        unset($data[0]['alreadysubmitnumber']);
        unset($data[0]['difficulty']);
		
        $data[0]['taskStatus']= $taskStatus;
        $data[0]['polltid']= $polltid;
        $data[0]['isPolishedBest']= $isPolishedBest;
        $data[0]['curUserAlreadyVoted']= $curUserAlreadyVoted;
		   
//       echo '<pre>';
//       print_r($data);die;
       #加上总浏览数
//       $all_browse=pre_forum_thread::all_browse($param);
       $all_browse=pre_forum_thread::all_browse($data[0]);
       $data[0]['count_browse']=$all_browse[0]->views;
       #加上总评论数
       $count_comment=pre_forum_post::count_comment($data[0]);
       $data[0]['count_comment']=$count_comment[0]->count;
       #加上总分
       $grade=pre_ct_translatedtext::count_grade($data[0]);
       if(!empty($grade))
       {
           $data[0]['count_grade']=$grade[0]->ctcredit;
       }
       else
       {
           $data[0]['count_grade']=0;
       }

       $data = array(
           'code' => 200,
           'msg' => '用户翻译详情数据返回成功',
           'result' => $data[0],

       );
       exit(json_encode($data));

   }

   #中英文交叉合并数组
   public function  arr_info($list,$list2)
   {
       $size = count($list) > count($list2) ? count($list) : count($list2); //取出元素最多的数组循环
       $arr = array();
       for($i=0;$i < $size; $i++){
           array_push($arr,$list[$i]); //将数组压入新的变量
           array_push($arr,$list2[$i]);//将数组压入新的变量
       }
       return $arr;
   }

    #针对某个翻译用户译文的评价用户 头像，用户名，评价详情 评论时间 分页列表（分页）
    #传tid
    #pid
    public  function comment_list()
    {
        $p = $_REQUEST;
        $page =isset($p['page']) ?  (int)$p['page'] : '';
        $num = isset($p['num']) ?  (int)$p['num'] : '';
        //$uid=isset($p['uid']) ?  (int)$p['uid'] : '';
        $pid=isset($p['pid']) ?  (int)$p['pid'] : '';
        $tid=isset($p['tid']) ?  (int)$p['tid'] : '';

        if( empty($tid) ||empty($pid) )
        {
            $data = array(
                'code' => 5,
                'msg' => '参数不全',
            );
            exit(json_encode($data));
        }

		if ( $pid == -1 ){
			$tmp_param = array('tid'=>$tid);
			$r = pre_forum_post::get_pid_forum_post($tmp_param);
			unset($tmp_param);
			$pid = $r[0]->pid;
		}
		
        $num=!empty($num) ? $num : 10;
        $page=!empty($page) ? $page : 1;
        $limit_start=($page-1)*$num;
        $limt_end= $num;
        //$param=['uid'=>$uid,'tid'=>$tid,'pid'=>$pid,'start'=>$limit_start,'end'=>$limt_end];
		$param=['tid'=>$tid,'pid'=>$pid,'start'=>$limit_start,'end'=>$limt_end];
        $data=$this->comment_data($param,$page,$num);

        if(empty($data['result']))
        {
            $data = array(
                'code' => 200,
                'msg' => '亲，暂无数据',
                'result' => []
            );

        }
        exit(json_encode($data));
    }

    public function comment_data($param,$page,$num)
    {

        $data=pre_forum_post::comment_list($param);
        $data=json_decode(json_encode($data),true);
        $count=pre_forum_post::comment_count($param);
        $count=json_decode(json_encode($count),true);
        if(empty($data))
        {
            $data2=[];
            $count=array(0=>array('count'=>1,'page'=>1));
            $data2['count']=$count[0]['count'];
            $data2['page']=$count[0]['page'];
            $result=[];

        }
        else
        {
            $data2=[];
            $count=ceil($count[0]['count']/$num);
            $count=array(0=>array('count'=>$count,'page'=>$page));
            $data=$this->head_img($data);
            $data2['count']=$count[0]['count'];
            $data2['page']=$count[0]['page'];
			foreach($data as $k=>$v){
				$data[$k]['message'] = discuzcode($v['message'],1,0);
			}
            $result=$data;
        }

        $data1 = array(
            'code' => 200,
            'msg' => '数据返回成功',
            'count'=>$data2['count'],
            'page'=>$data2['page'],
            'result' => $result
        );
        return $data1;
    }

    #获取评论用户头像
    public function  head_img($data)
    {
        foreach($data as $k=>$v)
        {
            $data[$k]['head_img']=avatarUrl($v['authorid']);
            $data[$k]['dateline']=date('Y-m-d H:i:s',$v['dateline']);
        }
        return $data;
    }

   #用户投票
    #传uid 和tid  翻译人用户名
   public  function vote()
   {
       $p = $_REQUEST;
       $uid=isset($p['uid']) ?  (int)$p['uid'] : '';
       $tid=isset($p['tid']) ?  (int)$p['tid'] : '';
       $author=isset($p['author']) ?  addslashes(trim($p['author'])) : '';

       if( empty($tid) ||empty($uid) || empty($author))
       {
           $data = array(
               'code' => 5,
               'msg' => '参数不全',
               'result' => [],
           );
           exit(json_encode($data));
       }
       $param=['uid'=>$uid,'tid'=>$tid];
       $r=pre_forum_pollvoter::find($param);
       if($r)
       {
           $data = array(
               'code' => 200,
               'msg' => '亲，您已经投过票了',
               'result' => [],
           );
           exit(json_encode($data));
       }
       else
       {
           $param['tid']=$tid;
           $param['polloption']=$author;
           $param['voterids']=$uid;
           $param['author']=$author;
           $data = pre_forum_pollvoter::modify($param);
           $arr['tid']=$tid;
           $arr['uid']=$uid;
           $arr['username']=$author;
           $arr['options']=$data[1];
           $arr['dateline']=time();
           $r = pre_forum_pollvoter::add($arr);
           if($r)
           {
               $data1 = array(
                   'code' => 200,
                   'msg' => '亲，投票成功，感谢您支持',
                   'result' => [],
               );
               exit(json_encode($data1));
           }
       }

   }

   #校验登录
    public  function  check_login()
    {
        #①先判断是否已经登录
        session_start();
        if(!$_SESSION)
        {
            $data = array(
                'code' => 3,
                'msg' => '亲，请登录',
                'result' => [],
            );
            exit(json_encode($data));
        }
    }

    #显示投票列表
    public function vote_list()
    {
        #①先判断是否已经登录
        $this->check_login();
        /* 投票不需要实名认证，所以注释掉下面的代码
		$check=pre_forum_pollvoter::check((int)$_SESSION['uid']);
        if(!$check)
        {
            $data = array(
                'code' => 4,
                'msg' => '亲，请完成实名认证',
                'result' => [],
            );
            exit(json_encode($data));
        }
		*/
        $p = $_REQUEST;
        $tid=isset($p['tid']) ?  (int)$p['tid'] : '';
        $uid=$_SESSION['uid'];

        if(empty($tid))
        {
            $data = array(
                'code' => 5,
                'msg' => '参数不全',
                'result' => [],
            );
            exit(json_encode($data));
        }
        $param=['uid'=>$uid,'tid'=>$tid];
        $r=pre_forum_pollvoter::find($param);
        if($r)
        {
            $data = array(
                'code' => 6,
                'msg' => '亲，您已经投过票了',
                'result' => [],
            );
        }
        else
        {
            $pollInfo = pre_forum_pollvoter::getPollInfoByPolltid($param['tid']);
			$vote_list=pre_forum_pollvoter::option($param);
            $data = array(
                'code' => 200,
                'msg' => '投票列表返回成功',
                'result' => array('voterCount'=>$pollInfo[0]->voters, 'polloptions'=>$vote_list),
            );
        }
        exit(json_encode($data));
    }

    #提交投票数据
    public function commit_vote()
    {
        #①先判断是否已经登录
        $this->check_login();
        #查看是否已经投票
        $p = $_REQUEST;
		
        $tid=isset($p['tid']) ?  (int)$p['tid'] : '';
        $vote_id_str =isset($p['vote_ids']) ?  addslashes(trim(($p['vote_ids']))) : '';
		$vote_ids = explode(',',$vote_id_str);

        if(empty($tid) || empty($vote_ids))
        {
            $data = array(
                'code' => 5,
                'msg' => '参数不全',
                'result' => [],
            );
            exit(json_encode($data));
        }

		$curUser=getCurUser();
        if(empty($curUser))
        {
            $data = array(
                'code' => 3,
                'msg' => '亲，请登录',
                'result' => [],
            );
            exit(json_encode($data));
		}
		$uid=$curUser['uid'];
 
       $param['uid']=$uid;
        $param['tid']=$tid;
        $vote_status=pre_forum_pollvoter::find($param);
        if($vote_status)
        {
            $data = array(
                'code' => 6,
                'msg' => '亲，您已经投过票了',
                'result' => [],
            );
        }
        else
        {
            pre_forum_pollvoter::voting_process($vote_ids,$tid);
            $data = array(
                'code' => 200,
                'msg' => '亲，投票成功',
                'result' =>[],
            );
        }
        exit(json_encode($data));
    }

    #展示投票结果
    public function  vote_result()
    {
        $p = $_REQUEST;
        $tid=isset($p['tid']) ?  (int)$p['tid'] : '';
        if(empty($tid))
        {
            $data = array(
                'code' => 5,
                'msg' => '参数不全',
                'result' => [],
            );
            exit(json_encode($data));
        }
		
		$curUser=getCurUser();
        if(empty($curUser))
        {
            $data = array(
                'code' => 3,
                'msg' => '亲，请登录',
                'result' => [],
            );
            exit(json_encode($data));
		}
		$uid=$curUser['uid'];
		
        $param=['tid'=>$tid,'uid'=>$uid];
        $r=pre_forum_pollvoter::vote_display($param);
        $data = array(
            'code' => 200,
            'msg' => '亲，投票结果展示返回成功',
            'result' =>$r[0],
        );

        exit(json_encode($data));


    }


    #更新浏览信息
    public  function  browse()
    {
        $p = $_REQUEST;
        $tid=isset($p['tid']) ?  (int)$p['tid'] : '';
        if( empty($tid) )
        {
            $data = array(
                'code' => 5,
                'msg' => '参数不全',
            );
            exit(json_encode($data));
        }
        $r=pre_forum_thread::modify($tid);
        if($r)
        {
            $data = array(
                'code' => 200,
                'msg' => '更新浏览信息成功',
                'result' => [],
            );
            exit(json_encode($data));
        }
        else
        {
            var_dump($r);
        }

    }

    #任务列表 状态（1,2,3)
//test cases:
//status=1 api/translation/task_list?status=1	(共1条数据)
//status=2 api/translation/task_list?status=2  (共1条数据)
//status=3 api/translation/task_list?status=3  (共16条数据，本页10条，8/29-9/17，注意status=3会返回seriesList)
//status=3 第2页：api/translation/task_list?status=3&page=2 (共16条数据，本页6条，7/21-8/29)
//status=3 按照系列筛选：api/translation/task_list?status=3&seriesId=1 (共4条数据，7/21,9/13,9/13,9/17)
//status=3 按照系列筛选：api/translation/task_list?status=3&seriesId=2 (共4条数据，8/29)
//status=3 api/translation/task_list?status=3&seriesId=2&page=2 (无数据，正常情况下不会，属于rainy day scenario)
//status=3 按照开始时间筛选：api/translation/task_list?status=3&start_time=2017-8-29 (共14条数据) 
//status=3 按照开始时间筛选, page=2：api/translation/task_list?status=3&start_time=2017-8-29&page=2 (共4条数据) 
//status=3 按照结束时间筛选：api/translation/task_list?status=3&end_time=2017-8-29 (共13条数据,本页10条) 
//status=3 按照结束时间筛选，第2页：api/translation/task_list?status=3&end_time=2017-8-29&page=2 (共13条数据,本页3条)
//status=3 按照开始和结束时间筛选：api/translation/task_list?status=3&start_time=2017-8-29&end_time=2017-8-29 (共11条数据,本页10条) 
//status=3 按照开始和结束时间筛选，第2页：api/translation/task_list?status=3&start_time=2017-8-29&end_time=2017-8-29&page=2 (共11条数据,本页1条) 
//status=3 按照系列、开始和结束时间筛选：api/translation/task_list?status=3&seriesId=1&start_time=2017-8-29&end_time=2017-9-14(共2条数据) 
//status=3 按照开始和结束时间筛选，第2页，内部参数num：api/translation/task_list?status=3&page=2&start_time=2016-10-1&end_time=2017-11-11&num=2(共8页，本页2条数据) 
//空列表：api/translation/task_list?status=3&page=1&start_time=2017-11-11&end_time=2017-11-11 
	
    public  function  task_list()
    {
        $p = $_REQUEST;
        $page =isset($p['page']) ?  (int)$p['page'] : '';
        $num = isset($p['num']) ?  (int)$p['num'] : '';
        $status=isset($p['status']) ?  (int)$p['status'] : '';
		$seriesId = isset($p['seriesId']) ?  (int)$p['seriesId'] : -1;
        $start_time=isset($p['start_time']) ?  strtotime($p['start_time']) : '';
        $end_time=isset($p['end_time']) ?  strtotime($p['end_time']) : '';

        if( empty($status) )
        {
            $data = array(
                'code' => 5,
                'msg' => '参数不全',
            );
            exit(json_encode($data));
        }
        $num=!empty($num) ? $num : 10;
        $page=!empty($page) ? $page : 1;
        $limit_start=($page-1)*$num;
        $limt_end= $num;
        $param=['status'=>$status,'start'=>$limit_start,'end'=>$limt_end,'seriesId'=>$seriesId,'start_time'=>$start_time,'end_time'=>$end_time];
        $data=$this->task_data($param,$page,$num);
		//var_dump($data);
		//exit;
		
		if(empty($data['result']))
        {
            $data = array(
                'code' => 200,
                'msg' => '亲，暂无数据',
                'result' => [],
				'taskListDataEmptyUrl' => config('applqyw.discuz_server_url').'template/comiis_nby/img/lqyw_jing_qing_qi_dai.png',
            );

        }
        exit(json_encode($data));

    }

    public function task_data($param,$page,$num)
    {

        $data=pre_ct_task::task_list($param);
		//var_dump($data);
        $data=json_decode(json_encode($data),true);
		//var_dump($data);
        $count=pre_ct_task::task_count($param);
		
        //$count=json_decode(json_encode($count),true);
		
        if(empty($data)) 
        {
            $data2=[];
            $count=array(0=>array('count'=>1,'page'=>1));
            $data2['count']=$count[0]['count'];
            $data2['page']=$count[0]['page'];
            $result=[];

        }
        else
        {
            $data2=[];
			//echo $count[0]->{'count'}."</br>";
            $count=ceil($count[0]->{'count'}/$num);
			//echo $count."</br>";
			//exit;
            $count=array(0=>array('count'=>$count,'page'=>$page));

            $data=$this->task_info($data);
            $data2['count']=$count[0]['count'];
            $data2['page']=$count[0]['page'];

			foreach($data as $v){
				$v['message'] = messagecutstr(str_replace("[//]", '', strip_tags($v['message'])));
			}
            $result=$data;
        }
        $data1 = array(
            'code' => 200,
            'msg' => '数据返回成功',
            'count'=>$data2['count'],
            'page'=>$data2['page'],
            'result' => $result
        );
		if($param['status']==3){
			$seriesList = pre_ct_task::getSeriesList($param['status']);
			$data1 []= array('seriesList' => $seriesList);
		}
        return $data1;
    }


    public function  task_info($data)
    {
        foreach($data as $k=>$v)
        {
            $data[$k]['difficulty']=$this->rank($v['difficulty']);
            $data[$k]['dateline']=date('Y-m-d H:i:s',$v['dateline']);
            $data[$k]['message']=mb_substr($v['message'],0,45,'utf-8').'......';
            
			if($v['status']==1)
            {
                //$data[$k]['submittime']=$this->totime(date('Y-m-d H:i:s',$v['submittime']));
				$data[$k]['submittime']=date('Y-m-d H:i:s',$v['submittime']);
            }
            else
            {
                //$data[$k]['commentendtime']=$this->totime(date('Y-m-d H:i:s',$v['commentendtime']));
				$data[$k]['commentendtime']=date('Y-m-d H:i:s',$v['commentendtime']);
            }
			
            $count=pre_ct_task::get_count($v['taskid']);
            $data[$k]['total_translators']=$count[0]->count;
            #加上总浏览数
            $all_browse=pre_forum_thread::all_browse($v);
            $data[$k]['count_browse']=$all_browse[0]->views;
        }
        return $data;
    }

}

